package zzx;

import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;

public class AuthoritySearch {
    private static List<Privi> priviList = new ArrayList<Privi>();
    private static List<Role> roleList = new ArrayList<Role>();
    private static List<User> userList = new ArrayList<User>();

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        // input Privilege list
        int p = cin.nextInt();
        for (int i = 0; i < p; i++) {
            priviList.add(new Privi(cin.next()));
        }
        // input role list
        int r = cin.nextInt();
        for (int i = 0; i < r; i++) {
            //
            Role ro = new Role(cin.next());
            int pn = cin.nextInt();
            for (int j = 0; j < pn; j++) {
                ro.addPrivi(new Privi(cin.next()));
            }
            roleList.add(ro);
        }
        // input user list;
        int u = cin.nextInt();
        for (int i = 0; i < u; i++) {
            User us = new User(cin.next());
            int rn = cin.nextInt();
            for (int j = 0; j < rn; j++) {
                us.addRole(roleFactory(cin.next()));
            }
            userList.add(us);
        }
        //权限查询
        int q = cin.nextInt();
        List<Query> queryList = new ArrayList<Query>();
        for (int i = 0; i < q; i++) {
            Query qu = new Query(cin.next(),cin.next());
            queryList.add(qu);
        }
        //query
        for (Query query : queryList) {
            System.out.println(check(query));
        }
        cin.close();
    }
    /**
     *
     */
    private static Role roleFactory(String name) {
        for(Role role : roleList) {
//            此处匹配的是角色名
            if(name.equals(role.getName())) {
                return role;
            }
        }
        return null;
    }

    /**
     */
    private static String check(Query query) {     //很绕
        int flag = -3;
        for (User user : userList) {
            flag = -3;
            if (query.qUserName.equals(user.getName())) {   // 匹配姓名后进入用户列表
                List<Role> userRoleList = user.getRoleList();
                for (Role role : userRoleList) {         //进入用户后，进入角色列表
                    List<Privi> userRolePriviList = role.getPriviList();
                    for (Privi privi : userRolePriviList) {    // 进入角色列表后，进入权限列表
//                        注意后面equals方法有重写来判断并返回正确的权限等级
                        int temp = query.qPrivi.equals(privi);    // 查询权限列表，匹配是否有此权限，并返回该权限的等级
                        flag = (temp > flag) ? temp : flag;
                        if(flag == -1) break;//跳出权限列表，已经查到了
                    }
                    if(flag == -1) break;//跳出角色列表，因为已经查到了
                }
            }
            if (flag != -3) {       //用户匹配成功后改变，查到了权限，跳出用户表
                break;
            }
        }
//        查询结果输出，只有有等级权限但是输入时没有说明该权限的等级的需要输出该权限等级
        if (flag == -2 || flag == -3) {    //权限为-2和-3都是结果失败，-2失败是用户没有该权限
            return "false";
        } else if(flag == -1) {  //有权限等级需要多匹配一次数字，否则看权限等级，对了就flag=-1
            return "true";
        } else
            return flag + "";
    }

    static class Query {
        public String qUserName;
        public Privi qPrivi;
        Query() {
            qUserName = "";
            qPrivi = new Privi();
        }
        Query(String username) {
            this.qUserName = username;
            this.qPrivi = new Privi();
        }
        Query(String username, String Priviname) {
            this.qUserName = username;
            this.qPrivi = new Privi(Priviname);
        }
    }

    static class Privi {
        private String name;
        private int level;
        public Privi() {}

        public Privi(String per) {
            String[] p = per.split(":");
            if (p.length == 1) {     //未说明权限等级时，将权限等级设为-1
                Privi.this.name = p[0];
                Privi.this.level = -1;
            } else if (p.length == 2) {    // 有权限等级时，权限等级大于0
                Privi.this.name = p[0];
                Privi.this.level = Integer.parseInt(p[1]);
            }
        }

        public String getName() {
            return this.name;
        }
        public void setName(String s) {
            this.name = s;
        }
        public int getLevel() {
            return this.level;
        }
        public void setLevel(int l) {
            this.level = l;
        }

//        注意此处重写了equal（）方法
        public int equals(Privi p) {     // 用查询权限匹配权限列表，注意p代表的是权限列表的权限
            if (this.name.equals(p.getName())) {       //权限列表找到了该权限
                if(this.level == -1 && this.level < p.getLevel())
//                    返回该权限等级
                    return p.getLevel();
                else if(this.level == -1 && p.getLevel() == -1)
                    return -1;//bufendengji
                else if(this.level != -1 && this.level <= p.getLevel() )
                    return -1;//fendengji
                else
                    return -2; //权限等级不匹配
            } else
                return -2;      //权限名不匹配
        }

        public String toString() {
            return this.name +(this.level != -1 ? (":" + this.level) : "");
        }
    }

    static class Role {
        private String name;
        private List<Privi> priList;

        public Role() {
            this.name = "";
            this.priList = new ArrayList<Privi>();
        }
        public Role(String n) {
            this.name = n;
            this.priList = new ArrayList<Privi>();
        }

        public String getName() {
            return this.name;
        }
        public void setName(String s) {
            this.name = s;
        }
        public int getPriviCount() {
            return this.priList.size();
        }
        public List<Privi> getPriviList() {
            return this.priList;
        }
        public void addPrivi(Privi p) {
            this.priList.add(p);
        }

        public String toString() {
            String s = this.name + "~" ;
            for(Privi p : this.priList) {
                s += p.toString() + "+";
            }
            return s;
        }
    }


    static class User {
        private String name;
        private List<Role> roleList;

       /* public User() {
            this.name = "";
            this.roleList = new ArrayList<Role>();
        }*/
        public User(String name) {
            this.name = name;
            this.roleList = new ArrayList<Role>();
        }

        public String getName() {
            return this.name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public List<Role> getRoleList() {
            return this.roleList;
        }
        public void addRole(Role role) {
            this.roleList.add(role);
        }
        public String toString() {
            String s = name + "~";
            for(Role role : roleList) {
                s += role.toString() + "+";
            }
            return s;
        }
    }
}